<?php
namespace app\beautiful\logic\order;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\StoreGoodsModel;
use app\beautiful\model\order\StoreOrderModel;

class OrderStoreLogic{
    use OrderTrait;
    protected $order_data=[];

    public function createOrder($params)
    {
        //判断用户
        empty($params['member_id']) && $this->error('请先登录后再操作');
        $this->checkMember($params['member_id'], false);

        //判断商品
        if(empty($params['goods']) || !is_array($params))  $this->error('请选择商品');
        foreach ($params['goods'] as $k => $v){
            $this->checkGoods($v,false);
        }

        //保存订单
        $order = new StoreOrderModel();

        $res=$order->saveAll($this->order_data);
        $order_ids=$res->column('store_order_id') ?? [];

        return $order_ids;
    }

    public function cancelOrder($params)
    {
        $this->checkMember($params['member_id'],false);
        $this->checkStoreOrder($params['order_id'],true);

        if($this->order->is_delete != 0) $this->error('订单不存在');
        if($this->order->status != 1) $this->error('订单无法取消');

        $this->order->status=-1;
        $this->order->save();

        $this->cancelGoods($this->order->store_goods_id,true);
    }

    public function createFakeOrder($params)
    {
        //判断用户
        empty($params['member_id']) && $this->error('请先登录后再操作');
        $this->checkMember($params['member_id'], false);

        //判断商品
        if(empty($params['goods']) || !is_array($params['goods']))  $this->error('请选择商品');
        if(count($params['goods']) >1) $this->error('只允许添加一个商品');

        if(empty($params['need_pay']) || $params['need_pay']<=0) $this->error('请输入实付金额');
        $this->checkGoodsFromFakeOrder($params['goods'][0],$params['need_pay'],$params['status'],false);

        //保存订单
        $order = new StoreOrderModel();

        $order->save($this->order_data);

        return $order->store_order_id;
    }

    protected function checkStoreOrder($orderId,$lock)
    {
        empty($orderId) &&  $this->error('请选择要支付的订单');
        if(null === $this->order = StoreOrderModel::where('store_order_id',$orderId)->lock($lock)->find()){
            $this->error('订单不存在');
        }
        if($this->member->shop_id != $this->shopId){
            $this->error('订单不存在');
        }
    }


    public function checkGoods($datas,$lock)
    {
        empty($datas['goods_id']) && $this->error('参数错误#3');
        $store_goods=StoreGoodsModel::where([
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['store_goods_id','=',$datas['goods_id']]
        ])->field('store_goods_id,goods_id,num,total_price,worth,gift_giving,take_count,fictitious_take_count')
            ->lock($lock)->find();
        $store_goods === null && $this->error('请选择商品');

        $goods=GoodsModel::find($store_goods->goods_id);
        empty($goods) && $this->error('商品不存在');

        //处理商品
        $store_goods->take_count=$store_goods->take_count + $store_goods->num;
        $store_goods->fictitious_take_count=$store_goods->fictitious_take_count + $store_goods->num;
        $store_goods->save();


        //处理订单商品表
        $this->order_data[]=[
            'shop_id'=>$this->shopId,
            'member_id'=>$this->member->member_id,
            'store_goods_id'=>$datas['goods_id'],
            'need_pay'=>$store_goods->total_price,
            'goods_name'=>$goods->name,
            'goods_cover_img'=>$goods->cover_img,
            'goods_num'=>$store_goods->num,
            'goods_total_price'=>$store_goods->total_price,
            'goods_worth'=>$store_goods->worth,
            'goods_gift_giving'=>$store_goods->gift_giving,
        ];

    }

    protected function cancelGoods($store_goods_id,$lock)
    {
        $store_goods=StoreGoodsModel::where([
            ['shop_id','=',$this->shopId],
            ['store_goods_id','=',$store_goods_id]
        ])->field('store_goods_id,goods_id,num,take_count,fictitious_take_count')
            ->lock($lock)->find();
        $store_goods === null && $this->error('请选择商品');

        $goods=GoodsModel::find($store_goods->goods_id);
        empty($goods) && $this->error('商品不存在');

        //处理商品
        $store_goods->take_count=$store_goods->take_count - 1;
        $store_goods->fictitious_take_count=($store_goods->fictitious_take_count - 1) < 0 ? $store_goods->fictitious_take_count : $store_goods->fictitious_take_count - 1;
        $store_goods->save();
    }

    public function checkGoodsFromFakeOrder($datas,$need_pay,$status,$lock)
    {
        empty($datas['goods_id']) && $this->error('参数错误#3');
        $store_goods=StoreGoodsModel::where([
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['store_goods_id','=',$datas['goods_id']]
        ])->field('store_goods_id,goods_id,num,total_price,worth,gift_giving,take_count,fictitious_take_count')
            ->lock($lock)->find();
        $store_goods === null  && $this->error('请选择商品');

        $goods=GoodsModel::find($store_goods->goods_id);
        empty($goods) && $this->error('商品不存在');

        //处理商品
        $store_goods->take_count=$store_goods->take_count + 1;
        $store_goods->fictitious_take_count=$store_goods->fictitious_take_count + 1;
        $store_goods->save();

        $youhui_balance=round((($store_goods->total_price)*100  - $need_pay*100) /100,2);

        //处理订单商品表
        $this->order_data=[
            'shop_id'=>$this->shopId,
            'member_id'=>$this->member->member_id,
            'store_goods_id'=>$datas['goods_id'],
            'need_pay'=>$need_pay,
            'goods_name'=>$goods->name,
            'goods_cover_img'=>$goods->cover_img,
            'goods_num'=>$store_goods->num,
            'goods_total_price'=>$store_goods->total_price,
            'goods_worth'=>$store_goods->worth,
            'goods_gift_giving'=>$store_goods->gift_giving,
            'pay_type'=>'offline',
            'youhui_balance'=>$youhui_balance > 0 ? $youhui_balance : 0,
            'is_paid'=>1,
            'pay_time'=>time(),
            'status'=>$status,
        ];
    }
}